import express from 'express';
import { fomaterStr } from '../../util/util.js'
import * as articles from '../../controllers/articles.js'
import * as comments from '../../controllers/comment.js'

const router = express.Router();

// router.get('/', (req, res) => {
//   res.send('This is the API root!');
// });

router.get("/hotpic",async (req,res)=>{
  let [ { count } ] = await articles.selectArticlesCount();
  
  // [0,1)  => [0,220) => [0,219)
  let start = Math.floor(Math.random()*count - 5);
  let data = await articles.selectArticlesRandom(start);

  res.send({
    code:200,
    msg:"查询成功",
    data
  })
});

// 根据前台传递的 文章 id 查询文章详情
router.get("/article",async (req,res)=>{
  // 获取请求携带参数，参数传递是通过地址参数方式传递(url参数,查询参数,get参数)
  let id = req.query.id; 
  if(id==null||id==""){
    // 传递的参数存在问题
    res.send({
      code:400,
      msg:"文章id参数有误"
    });
    return;
  }
  // 查询数据库
  // 基于文章id查询数据
  let [ data ] = await articles.selectArticleDetail(id);
  // 传递的参数不在合法范围内
  if(data==null){
    res.send({
      code:400,
      msg:"文章id参数有误"
    });
    return;
  }
  let [ preData ] = await articles.selectPreArticle(id);
  let [ nextData ] = await articles.selectNextArticle(id);

  // 组合返回结果的数据
  data.pre = preData===undefined?null:preData;
  data.next = nextData===undefined?null:nextData;
  // 返回查询结果
  res.send({
    code:200,
    msg:"查询成功",
    data
  })
});

router.get("/search",async (req,res)=>{
  let page = req.query.page;
  if(page==null||page==""){
    page = 1;
  }
  let perpage = req.query.perpage;
  if(perpage==null||perpage==""){
    perpage = 6;
  }
  // 计算从第几条开始进行查询
  let startIndex = (page-1)*perpage

  let type = req.query.type;
  let key = req.query.key;
  // let data = await articles.selectConditionArticle(startIndex,perpage,type,key);

  let condition = {
    equies:{},
    like:{}
  };
  if(type!=null && type!=""){
    condition.equies.categoryId = type;
  }
  if(key!=null && key!=""){
    condition.like.title = key;
  }

  let data = await articles.selectConditionArticle2(startIndex,perpage,condition);

  // for (let i = 0; i < data.length; i++) {
  //   let str = data[i].content.replace(/<\/?[^>]+(>|$)/g,"").substring(0,500);
  //   data[i].content = str;
  // }

  // for (let article of data) {
  //   // console.log(article.content);
  //   // +替换标签  +数据截取
  //   let str = article.content.replace(/<\/?[^>]+(>|$)/g,"").substring(0,500);
  //   // console.log(str);
  //   // 引用类型指向得关系
  //   // 通过引用类型的地址对内容进行了修改，也会导致原始的data变量的值一起变化
  //   article.content = str;
  // }

  for (let article of data) {
    article.content = fomaterStr(article.content,/<\/?[^>]+(>|$)/g,"",500);
  }

  res.send({
    code:200,
    msg:"查询成功",
    data
  });
});


router.post("/post_comment",async (req,res)=>{
  let author = req.body?.author ?? "";
  let content = req.body?.content ?? "";
  let articleId = req.body?.articleId ?? "";
  console.log(author,content,articleId);
  
  let [ { count } ] = await articles.selectArticlesCount();
  if(
    author=="" ||
    content=="" ||
    articleId=="" || articleId<1 || articleId>count
  ){
    res.send({
      code:400,
      msg:"参数错误"
    });
    return;
  }

  await comments.insertNewComment(author,content,articleId);

  res.send({
    code:201,
    msg:"评论发表成功"
  })
});

export default router;
